package com.linln.admin.member.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linln.common.utils.DateUtils;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.member.domain.MemberDayCount;
import com.linln.modules.member.domain.MemberPayDayCount;
import com.linln.modules.member.service.MemberDayCountService;
import com.linln.modules.member.service.MemberPayDayCountService;
import com.linln.modules.message.domain.MessageDayCount;
import com.linln.modules.message.service.MessageDayCountService;
import com.linln.modules.store.domain.StoreDayCount;
import com.linln.modules.store.service.StoreDayCountService;

/**
 * @author hct
 * @date 2019/09/12
 */
@Controller
@RequestMapping("/member/day")
public class MemberDayCountController {

    @Autowired
    private MemberDayCountService dayCountService;
    @Autowired
    private MessageDayCountService messageDayCountService;
 
    @Autowired
    private StoreDayCountService storeDayCountService;
 
    @Autowired
    private MemberPayDayCountService payDayCountService;
   
    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/list")
    @ResponseBody
    public ResultVo list(String area) {
        // 复制保留无需修改的数据
    	 // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();
        MemberDayCount memberDayCount=new MemberDayCount();
        // 获取数据列表
        Example<MemberDayCount> example = Example.of(memberDayCount, matcher);
        List<MemberDayCount> list=dayCountService.getList(example);
        Map<String,Integer> map=DateUtils.getDayMap1();
        //map
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        for (Map.Entry<String,Integer> m : map.entrySet()) {
        	if(list!=null && list.size()>0) {
        		for(MemberDayCount membercount:list) {

        			if(sdf.format(membercount.getCreateDate()).equals(m.getKey())) {
                        map.put(m.getKey(),map.get(m.getKey())+membercount.getNum());
//        				m.setValue(membercount.getNum());
        			}
        		}
         	}
            System.out.println("key:" + m.getKey() + " value:" + m.getValue());
        }
        
        return ResultVoUtil.success(map);
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/messagelist")
    @ResponseBody
    public ResultVo messagelist(HttpServletRequest request,String area) {
    	 // 创建匹配器，进行动态查询匹配
       // MemberInfo agent=(MemberInfo)request.getSession().getAttribute(AdminConst.AGENT_ROLE_NAME);
        // 复制保留无需修改的数据
    	 // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();
        MessageDayCount messageDayCount=new MessageDayCount();
        //messageDayCount.setArea(agent.getArea());
        // 获取数据列表
        Example<MessageDayCount> example = Example.of(messageDayCount, matcher);
        List<MessageDayCount> list=messageDayCountService.getList(example);
        Map<String,Integer> map=DateUtils.getDayMap1();
        //map
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        for (Map.Entry<String,Integer> m : map.entrySet()) {
        	if(list!=null && list.size()>0) {
        		for(MessageDayCount messagecount:list) {
        			
        			if(sdf.format(messagecount.getCreateDate()).equals(m.getKey())) {
                        map.put(m.getKey(),map.get(m.getKey())+messagecount.getNum());
//        				m.setValue(messagecount.getNum());
        			}
        		}
         	}
            System.out.println("key:" + m.getKey() + " value:" + m.getValue());
        }
        
        return ResultVoUtil.success(map); 
    }
    
    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/storelist")
    @ResponseBody
    public ResultVo storelist(HttpServletRequest request) {
    	 // 创建匹配器，进行动态查询匹配
       // MemberInfo agent=(MemberInfo)request.getSession().getAttribute(AdminConst.AGENT_ROLE_NAME);
        // 复制保留无需修改的数据
    	 // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();
        StoreDayCount messageDayCount=new StoreDayCount();
       // messageDayCount.setArea(agent.getArea());
        // 获取数据列表
        Example<StoreDayCount> example = Example.of(messageDayCount, matcher);
        List<StoreDayCount> list=storeDayCountService.getList(example);
        Map<String,Integer> map=DateUtils.getDayMap1();
        //map
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        for (Map.Entry<String,Integer> m : map.entrySet()) {
        	if(list!=null && list.size()>0) {
        		for(StoreDayCount messagecount:list) {
        			
        			if(sdf.format(messagecount.getCreateDate()).equals(m.getKey())) {
                        map.put(m.getKey(),map.get(m.getKey())+messagecount.getNum());
//        				m.setValue(messagecount.getNum());
        			}
        		}
         	}
            System.out.println("key:" + m.getKey() + " value:" + m.getValue());
        }
        
        return ResultVoUtil.success(map); 
    }
    
 
    
    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/paylist")
    @ResponseBody
    public ResultVo paylist(HttpServletRequest request) {
    	 // 创建匹配器，进行动态查询匹配
        //MemberInfo agent=(MemberInfo)request.getSession().getAttribute(AdminConst.AGENT_ROLE_NAME);
        // 复制保留无需修改的数据
    	 // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();
        MemberPayDayCount messageDayCount=new MemberPayDayCount();
       // messageDayCount.setArea(agent.getArea());
        // 获取数据列表
        Example<MemberPayDayCount> example = Example.of(messageDayCount, matcher);
        List<MemberPayDayCount> list=payDayCountService.getList(example);
        Map<String,Integer> map=DateUtils.getDayMap1();
        //map
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        for (Map.Entry<String,Integer> m : map.entrySet()) {
        	if(list!=null && list.size()>0) {
        		for(MemberPayDayCount messagecount:list) {
        			
        			if(sdf.format(messagecount.getCreateDate()).equals(m.getKey())) {
                        map.put(m.getKey(),map.get(m.getKey())+messagecount.getNum().intValue());
//        				m.setValue(messagecount.getNum()+"");
        			}
        		}
         	}
            System.out.println("key:" + m.getKey() + " value:" + m.getValue());
        }
        
        return ResultVoUtil.success(map); 
    }
}